#{extends 'main.html'/}
<div class="l-clear"></div>
<div id="maingrid" style="margin-top:1px"></div> <br />
<div style="display:none;"></div>
<div id="detail" style="display:none;"><form id="mainform" method="post"></form></div>

<script type="text/javascript">
    $(function () {
        var manager, grid;
        function f_initGrid() 
        {
            grid = manager = $("#maingrid").ligerGrid({
                columns: [
                    { display: '编号', name: 'id', width: 50, type: 'int' },
                    { display: '评价项名称', name: 'Names', width: 100, type: 'textarea' },
                    { display: '评价项内容', name: 'Measures', width: 100, type:'int'
                    },
                    { display: '标记', name: 'Flag', width: 120, type: 'string', type: 'textarea'},
                    { display: '等级', name: 'level', width: 120, type: 'string', type: 'textarea'},
                    { display: '排序号', name: 'orderNum', width: 120, type: 'string', type: 'textarea'}
                ],

                url: "appraiseType/json",  method: "GET",                       
                width: '100%',height:'50%',
                toolbar: { items: [
                        { text: '增加', click: addRow, icon: 'add' },
                        { line: true },
                        { text: '修改', click: modifyRow, icon: 'modify' },
                        { line: true },
                        { text: '删除', click: deleteRow, icon: 'delete' }
                    ] }
            });         	
        }
        
        f_initGrid();
        
        function f_reload() {
            grid.loadData();
        }
        
        function getSelected()
        { 
            var row = manager.getSelectedRow();
            if (!row) { alert('请选择行'); return; }
            alert(JSON.stringify(row));
        }
        
        function getData()
        { 
            var data = manager.getData();
            alert(JSON.stringify(data));
        }
        
        function addRow()
        {
            showDetail({}, false);
        }
        
        function modifyRow()
        {
            var selected = grid.getSelected();
            if (!selected) { LG.tip('请选择行!'); return }
            var parent = grid.getParent(selected);
            showDetail({            
                id: selected.id,
                Names: selected.Names,
                Measures: selected.Measures,
                Flag: selected.Flag,
                level: selected.level ,
                orderNum: selected.orderNum 
            }, false);
        }
        
        function deleteRow()
        {
            jQuery.ligerDialog.confirm('确定删除吗?', function (confirm) {
                if (confirm) f_delete(); });
        }
        
        function f_delete() {
            var selected = grid.getSelected();
            if (selected) {
                $.ajax({
                    url: 'appraiseTypes/' + selected.id,
                    type: 'DELETE',                  
                    loading: '正在删除中...',                  
                    success: function () {
                  
                        LG.tip('删除成功!');
                        f_reload();
                    },
                    error: function (message) {
                        LG.showError(message);
                    }
                });
            }
            else {
                LG.tip('请选择行!');
            }
        }
        
        var detailWin = null, curentData = null, currentIsAddNew;
        var mainform = $("#mainform");
	    
        function showDetail(data, isAddNew)
        {
            currentData = data;
            currentIsAddNew = isAddNew;
            if (detailWin)
            {
                detailWin.show(); 
            }
            else
            {
                //创建表单结构
              
                mainform.ligerForm({
                    inputWidth: 280,
                    fields: [
                        { name: "appraiseType.id", type: "hidden" },
                        { display: "评价项名称", name: "appraiseType.Names", newline: true, 
                            labelWidth: 100, width: 220, space: 30, type: "text", 
                            validate: { required: true, maxlength: 50} },
                        { display: "评价项内容", name: "appraiseType.Measures", newline: true, 
                            labelWidth: 100, width: 220, space: 30, type: "text" } ,
                        { display: "等级", name: "appraiseType.level", newline: true, 
                            labelWidth: 100, width: 220, space: 30, type: "text" },
                        { display: "标记", name: "appraiseType.Flag", newline: true, 
                            labelWidth: 100, width: 220, space: 30, type: "text" }   ,
                        { display: "排序号", name: "appraiseType.orderNum", newline: true, 
                            labelWidth: 100, width: 220, space: 30, type: "text" }                      
                    ],         			
                    toJSON: JSON2.stringify
                });
              
                //$("#DeptParentName").attr("readonly", "readonly");

                detailWin = $.ligerDialog.open({
                    target: $("#detail"),
                    width: 400, height: 300, top:80,
                    buttons: [
                        { text: '确定', onclick: function () { save(); } },
                        { text: '取消', onclick: function () { detailWin.hide(); } }
                    ]
                });
            }
          
            if (currentData)
            {    	  
                $("[name$=id]").val(currentData.id)
                $("[name$=Measures]").val(currentData.Measures);
                $("[name$=Names]").val(currentData.Names);
                $("[name='appraiseType.Type']").val(currentData.Type);
                $("[name$=Flag]").val(currentData.Flag);
                $("[name$=level]").val(currentData.level);
                $("[name$=orderNum]").val(currentData.orderNum);
            }

            function save()
            {
                $("#mainform").validate();
                if(!$("[name$=level]").val()){
                    LG.tip('等级不能为空!');
                    return;
                }
                  if(!$("[name$=Flag]").val()){
                    LG.tip('标记不能为空!');
                    return;
                }
                  if(!$("[name$=orderNum]").val()){
                    LG.tip('排序号不能为空!');
                    return;
                }
               
                
             
                $.ajax({
                    loading: '正在保存数据中...',
                    type: 'POST',
                    url: 'appraiseTypes',                  
                    data: $("#mainform").serialize(),
                    success: function ()
                    {
                        detailWin.hide();
                        grid.loadData();
                        LG.tip('保存成功!');
                    },
                    error: function (message)
                    {
                        LG.tip(message);
                    }
                });
            }
        }
   
    })
</script>
